#!/data/data/com.termux/files/usr/bin/bash
msfpath='/data/data/com.termux/files/usr/share/metasploit-framework'
rubypath='/data/data/com.termux/files/usr/share/ruby'
msfversion='6.1.2'
rubyversion='2.7.2'
echo "[*]开始更换国内源"
echo "出现选择请一侓选择 Y "
sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list
sed -i 's@^\(deb.*games stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/game-packages-24 games stable@' $PREFIX/etc/apt/sources.list.d/game.list
sed -i 's@^\(deb.*science stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/science-packages-24 science stable@' $PREFIX/etc/apt/sources.list.d/science.list
apt update && apt upgrade -y
sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list
sed -i 's@^\(deb.*games stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/game-packages-24 games stable@' $PREFIX/etc/apt/sources.list.d/game.list
sed -i 's@^\(deb.*science stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/science-packages-24 science stable@' $PREFIX/etc/apt/sources.list.d/science.list
apt update
echo "[*]开始下载metasploit-framework-${msfversion}依赖包"
sleep 2
apt install -y libiconv zlib autoconf bison clang coreutils curl findutils git apr apr-util libffi libgmp libpcap postgresql readline libsqlite openssl libtool libxml2 libxslt ncurses pkg-config wget make libgrpc termux-tools ncurses-utils ncurses unzip zip tar
echo "[*]开始删除旧版metasploit-framework"
sleep 2
rm -rf /data/data/com.termux/files/home/metasploit-framework /data/data/com.termux/files/usr/share/metasploit-framework
echo "[*]开始下载metasploit-framework归档包"
sleep 2
pkg i axel -y && axel -n 256 https://github.com.cnpmjs.org/rapid7/metasploit-framework/archive/6.1.2.tar.gz  -o ${TMPDIR}/
echo "[*]开始解压metasploit-framework归档包"
sleep 2
tar -xf ${TMPDIR}/metasploit-framework-6.1.2.tar.gz -C ${TMPDIR}/
mkdir -p ${msfpath}
cp -rf ${TMPDIR}/metasploit-framework-${msfversion}/* ${msfpath}/
echo "[*]开始下载ruby-${rubyversion}源码包"
sleep 2
mv ~/termux-msf-cn/ruby-2.7.2.tar.gz  ${TMPDIR}/
echo "[*]开始解压ruby-${rubyversion}源码包"
sleep 2
tar -xf ${TMPDIR}/ruby-2.7.2.tar.gz -C ${TMPDIR}/
echo "[*]开始配置ruby-${rubyversion}"
sleep 2
cd ${TMPDIR}/ruby-${rubyversion}/
./configure --prefix=/data/data/com.termux/files/usr/share/ruby/${rubyversion}/
echo "[*]开始编译${rubyversion}版本的ruby-可能比较久"
sleep 2
make -j8
echo "[*]开始安装${rubyversion}版本的ruby"
sleep 2
mkdir -p /data/data/com.termux/files/usr/share/ruby/${rubyversion}/
make install
make clean

echo "[*]开始更换ruby国内源"
sleep 2
echo 添加 TUNA 源并移除默认源

${rubypath}/${rubyversion}/bin/gem sources --add https://mirrors.tuna.tsinghua.edu.cn/rubygems/ --remove https://rubygems.org/

${rubypath}/${rubyversion}/bin/bundle config mirror.https://rubygems.org https://mirrors.tuna.tsinghua.edu.cn/rubygems

echo 列出已有源
${rubypath}/${rubyversion}/bin/gem sources -l
# 应该只有 TUNA 一个



echo "[*]开始安装metasploit-framework-${msfversion}"
sleep 2
cd ${msfpath}
${rubypath}/${rubyversion}/bin/gem install --no-document --verbose bundler:1.17.3
${rubypath}/${rubyversion}/bin/bundle config build.nokogiri --use-system-libraries
${rubypath}/${rubyversion}/bin/bundle install -j5
echo "[*]执行一些修复"
sleep 2
pkg i python  -y
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip install requests
sed -i "s@/etc/resolv.conf@$PREFIX/etc/resolv.conf@g" ${msfpath}/lib/net/dns/resolver.rb
echo "[*]修改metasploit-framework的默认解释器"
sleep 2
sed -i '1c#!'${rubypath}'/'${rubyversion}'/bin/ruby' ${msfpath}/msfconsole ${msfpath}/msfd ${msfpath}/msfdb ${msfpath}/msfrpc ${msfpath}/msfrpcd ${msfpath}/msfupdate ${msfpath}/msfvenom
echo "[*]开始配置数据库"
sleep 2
mkdir -p ${msfpath}/config/
mv ~/termux-msf-cn/database.yml ${msfpath}/config/
mkdir -p $PREFIX/var/lib/postgresql
pg_ctl -D $PREFIX/var/lib/postgresql stop > /dev/null 2>&1 || true 
if ! pg_ctl -D "$PREFIX"/var/lib/postgresql start --silent; then
    initdb "$PREFIX"/var/lib/postgresql
    pg_ctl -D "$PREFIX"/var/lib/postgresql start --silent
fi
if [ -z "$(psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='msf'")" ]; then
    createuser msf
fi
if [ -z "$(psql -l | grep msf_database)" ]; then
    createdb msf_database
fi
echo "[*]开始将metasploit-framework加入PATH路径"
sleep 2
echo "export PATH=$PATH:${msfpath}" >> $PREFIX/etc/profile
export PATH=$PATH:${msfpath}
echo "[*]处理安装文件"
sleep 1
rm -rf ${TMPDIR}/metasploit-framework-6.1.2.tar.gz ${TMPDIR}/ruby-${rubyversion}.tar.gz
apt clean
rm -rf ~/.cache/pip/*
cd
rm -rf termux-msf-cn
echo "[*]安装完成😂请重启termux"
exit
